package TreatmentRecordUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import persistent.*;
import LoginUtil.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db4o.ObjectContainer;
import java.lang.NumberFormatException;

public class ViewTreatmentRecordServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, java.io.IOException {
		try{
			ObjectContainer container = 
		        (ObjectContainer)request.getAttribute(Db4oServletListener.KEY_DB4O_SESSION);
			
			String patientName = request.getParameter("treatmentrecordname");
			String dateString = request.getParameter("treatmentrecorddate");
			
			//Get patient with user's name
			User userPredicate = new User();
			userPredicate.setUserName(patientName);
			
			User patient = (User)container.queryByExample(userPredicate).next();
			
			
			TreatmentRecord record = new TreatmentRecord(null, patient, null);
			
			DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
			record.setDate((Date)formatter.parse(dateString));
			
			TreatmentRecord viewedRecord = (TreatmentRecord)container.queryByExample(record).next();
			
			request.getSession().removeAttribute("KEY_VIEWING_RECORD");
			request.getSession().setAttribute("KEY_VIEWING_RECORD", viewedRecord);
			
			response.sendRedirect("modifyTreatmentRecord.jsp");
			
		}
		catch(Throwable theException){
			System.out.println(theException);
		}
	}
	
	
}